<?php
   $query_by_category = "select distinct p.products_id, p.products_image, p.products_tax_class_id, pd.products_name,
                                  p.products_date_added, p.products_price, p.products_type, p.master_categories_id
                           from " . TABLE_PRODUCTS . " p JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON p.products_id = pd.products_id JOIN ".
                           TABLE_PRODUCTS_TO_CATEGORIES." p2c ON p2c.products_id=p.products_id JOIN ".
                           TABLE_CATEGORIES." c ON c.categories_id = p2c.categories_id 
                           where pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                           and p.products_status = 1
                           and c.categories_id in (%s)
                           order by p.products_sort_order asc,p.products_id asc 
                           LIMIT 0,5";
                           
$sql = "select c.categories_id,cd.categories_name from ".TABLE_CATEGORIES." c 
		JOIN ".TABLE_CATEGORIES_DESCRIPTION." cd ON c.categories_id=cd.categories_id 
		where c.parent_id=0 and cd.language_id =".(int)$_SESSION['languages_id']." order by sort_order asc";
$r = $db->Execute($sql);
$new_products_by_category = array();
while (!$r->EOF){
	$categories_id = array();
	zen_get_subcategories($categories_id,$r->fields['categories_id']);
	if (!empty($categories_id)){
		$sql = sprintf($query_by_category,implode(',',$categories_id));
		$new_products_by_category[$r->fields['categories_name']] = $db->Execute($sql);
	}
	$r->MoveNext();
}
                          
                           